using System;
using System.Drawing;
using System.Security.Permissions;
using System.Web;

namespace PickGold.Charting
{
	[SRDescription("DescriptionAttributeTitles")]
	[AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
	[AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
	public class TitleCollection : ChartNamedElementCollection<Title>
	{
		internal TitleCollection(IChartElement parent) : base(parent)
		{
		}

		public Title Add(string name)
		{
			Title item = new Title(name);
			base.Add(item);
			return item;
		}

		internal static void CalcInsideTitlePosition(ChartPicture chartPicture, ChartGraphics chartGraph, float elementSpacing)
		{
			if (chartPicture != null)
			{
				foreach (Title title in chartPicture.Titles)
				{
					if (title.IsVisible() && (title.DockedToChartArea != "NotSet"))
					{
						try
						{
							ChartArea local1 = chartPicture.ChartAreas[title.DockedToChartArea];
							continue;
						}
						catch
						{
							throw new ArgumentException(SR.ExceptionChartTitleDockedChartAreaIsMissing(title.DockedToChartArea));
						}
					}
				}
				foreach (ChartArea area in chartPicture.ChartAreas)
				{
					if (area.Visible)
					{
						RectangleF chartAreasRectangle = area.PlotAreaPosition.ToRectangleF();
						float num = Math.Min((float)((chartAreasRectangle.Height / 100f) * elementSpacing), (float)((chartAreasRectangle.Width / 100f) * elementSpacing));
						foreach (Title title2 in chartPicture.Titles)
						{
							if ((title2.IsDockedInsideChartArea && (title2.DockedToChartArea == area.Name)) && title2.Position.Auto)
							{
								RectangleF empty = RectangleF.Empty;
								title2.CalcTitlePosition(chartGraph, ref chartAreasRectangle, ref empty, num);
							}
						}
						continue;
					}
				}
			}
		}

		internal static void CalcOutsideTitlePosition(ChartPicture chartPicture, ChartGraphics chartGraph, ChartArea area, ref RectangleF chartAreasRectangle, float elementSpacing)
		{
			if (chartPicture != null)
			{
				float num = Math.Min((float)((chartAreasRectangle.Height / 100f) * elementSpacing), (float)((chartAreasRectangle.Width / 100f) * elementSpacing));
				foreach (Title title in chartPicture.Titles)
				{
					if (!title.IsVisible())
					{
						continue;
					}
					if ((title.DockedToChartArea != "NotSet") && (chartPicture.ChartAreas.IndexOf(title.DockedToChartArea) < 0))
					{
						throw new ArgumentException(SR.ExceptionChartTitleDockedChartAreaIsMissing(title.DockedToChartArea));
					}
					if ((!title.IsDockedInsideChartArea && (title.DockedToChartArea == area.Name)) && title.Position.Auto)
					{
						RectangleF empty = RectangleF.Empty;
						RectangleF ef2 = chartAreasRectangle;
						title.CalcTitlePosition(chartGraph, ref chartAreasRectangle, ref empty, num);
						RectangleF ef3 = title.Position.ToRectangleF();
						if (title.Docking == Docking.Top)
						{
							ef3.Y -= num;
							if (!area.Position.Auto)
							{
								ef3.Y -= ef3.Height;
								ef2.Y -= ef3.Height + num;
								ef2.Height += ef3.Height + num;
							}
						}
						else if (title.Docking == Docking.Bottom)
						{
							ef3.Y += num;
							if (!area.Position.Auto)
							{
								ef3.Y = ef2.Bottom + num;
								ef2.Height += ef3.Height + num;
							}
						}
						if (title.Docking == Docking.Left)
						{
							ef3.X -= num;
							if (!area.Position.Auto)
							{
								ef3.X -= ef3.Width;
								ef2.X -= ef3.Width + num;
								ef2.Width += ef3.Width + num;
							}
						}
						if (title.Docking == Docking.Right)
						{
							ef3.X += num;
							if (!area.Position.Auto)
							{
								ef3.X = ef2.Right + num;
								ef2.Width += ef3.Width + num;
							}
						}
						title.Position.SetPositionNoAuto(ef3.X, ef3.Y, ef3.Width, ef3.Height);
						if (!area.Position.Auto)
						{
							chartAreasRectangle = ef2;
						}
					}
				}
			}
		}

		internal void ChartAreaNameReferenceChanged(object sender, NameReferenceChangedEventArgs e)
		{
			if (e.OldElement != null)
			{
				foreach (Title title in this)
				{
					if (title.DockedToChartArea == e.OldName)
					{
						title.DockedToChartArea = e.NewName;
					}
				}
			}
		}
	}
}

